<?php

namespace App\Http\Controllers;

use App\Http\Requests\IndexOrderRequest;
use App\Http\Requests\StoreOrderRequest;
use App\Http\Requests\UpdateOrderRequest;
use App\Http\Resources\OrderResource;
use App\Models\Order;
use Illuminate\Support\Facades\Auth;

class OrderController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }
    /**
     * Display a listing of the resource.
     */
    public function index(IndexOrderRequest $request)
    {
        $order = Order::query();
        if (!isSuperadmin()) {
            $order->where('admin_id', Auth::id());
        }
        $order->when($request->filled('order_number'), function ($order) use ($request) {
            $order_number = $request->input('order_number');
            return $order->where('order_number', 'like', "%$order_number%");
        })->when($request->filled('status'), function ($order) use ($request) {
            $status = $request->input('status');
            return $order->where('status', $status);
        })->when($request->filled('blind_name'), function ($order) use ($request) {
            $blind_name = $request->input('blind_name');
            return $order->whereHas('blinds', function ($query) use ($blind_name) {
                $query->where('name', 'like', "%$blind_name%");
            });
        });

        $data = $order->with(['users', 'blinds', 'shops', 'statuslabel'])->orderByDesc('created_at')->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );

        $response =  OrderResource::collection($data);
        $total = $response->total();
        return $this->success('查询成功', $response, $total);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreOrderRequest $request, Order $order)
    {
        // $order->fill($request->input());
        // $order->status = 1;
        // $order->save();
        // return $this->success('创建订单成功', $order);
    }

    /**
     * Display the specified resource.
     */
    public function show(Order $order)
    {
        $this->authorize('view', $order);
        $data = $order->load(['users', 'admins', 'blinds', 'shops', 'statuslabel']);
        return $this->success('查询成功', new OrderResource($data));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Order $order)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateOrderRequest $request, Order $order)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Order $order)
    {
        $this->authorize('delete', $order);
        $order->deleteOrFail();
        return $this->success('删除成功', $order);
    }
}
